var ctx;

function setCtx(ctx) {
	this.ctx = ctx;
}

//提示昵称不能为空
function checkNickName() {
	var nickName = $("#nickName").val();
	//解决ie9以下placeholder与value重复的问题
	if(nickName == $("#nickName").attr("placeholder")){
		nickName = "";
	}
	
	if (nickName == null || nickName == "") {
		layer.tips('昵称不能为空!', '#nickName', {
			tips : 3,
			time:1000
		});
		return false;
	} else {
		return true;
	}
}

//提示昵称不能为空
function checkUserName() {
	var userName = $("#userName").val();
	//解决ie9以下placeholder与value重复的问题
	if(userName == $("#userName").attr("placeholder")){
		userName = "";
	}
	
	if (userName == null || userName == "") {
		layer.tips('真实姓名不能为空!', '#userName', {
			tips : 3,
			time:1000
		});
		return false;
	} else {
		return true;
	}
}

//检验email格式是否正确
function ischeckemail() {
	var email = $("#email").val();
	  if (email != "") {
	     var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	     isok= reg.test(email );
	       if (!isok) {
	    	   layer.tips('email格式不正确!', '#email', {
	   			tips : 3,
	   			time:1000
	   		});
	    	   return false;
	        } else {
	        	return true;
	        }
	    } else if(email == ""){
	    	layer.tips('email不能为空!', '#email', {
	   			tips : 3,
	   			time:1000
	    	});
	    	return false
	    }
	}

//确定按钮
function savePersonInfo() {
	var nickName = $('#nickName').val();
	var userName = $('#userName').val();
	var sex = $("input[type=radio]:checked").val();
	var email = $('#email').val();
	var id = $('#id').val();
	
	
	
	if (ischeckemail() && checkNickName() && checkUserName()) {
		var index1 = layer.load(1);
		$.ajax({
			type : "POST",
			url : ctx + "/user/savePersonInfo",
			cache : false,
			dataType : 'json',
			data : {
				id : id,
				nickName : nickName,
				userName : userName,
				email : email,
				sex : sex
			},
			success : function(result) {
				if (result.success) {
					layer.msg(result.msg, {
						icon : 1,
						time : 1000
					// 2秒关闭（如果不配置，默认是3秒）
					}, function() {
						 layer.close(index1);
						window.location.href=ctx+"/main?item=6";
					});
				} else {
					layer.msg("操作失败", {
						icon : 2,
						time : 1000
					});
					 layer.close(index1);
				}
			}
		});
	}
}

//返回上一页
function cancel(){
	window.location.href=ctx+"/main?item=6";
}